<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台店铺控制器
 * 
 * @author Ringo Chan <334258471@qq.com>
 */
use Admin\Model\OrderModel;
class ShopController extends AdminController{
    
    public function index(){
        
    }
    
    /**
     * 异步获取店铺数据
     *
     * @param string query 店铺编号或名称
     * @return array 店铺编号、名称
     */
    public function autocomplete(){
        $title       =   I('query');
        
        $map['status']  =   array('egt',0);
        $map['review']  =   array('eq',1);
        if(is_numeric($title)){
            $map['id|title']=   array(intval($title),array('like','%'.$title.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$title.'%');
        }
        
        $list   = M('Shop')->where($map)->page(0, 5)->select();
        
        echo json_encode($list);
    }


    /**
     * 商家搜索和首页
     */
    public function manage(){

        $nickname       =   I('get.shopname');
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }
        $list=M('shop')->where($map)->order('create_time desc')->select();
        $map['review']=0;
        $list2=M('shop')->where($map)->order('create_time desc')->select();

        int_to_string($list);
        foreach ($list as &$item){
            $item['score'] = D('member')->where('uid='.$item['uid'] )->getField('score');
        }
        $this->assign('_list', $list);
        $this->assign('_total', count($list));
        $this->assign('_total2', count($list2));
        $this->meta_title = '店铺列表';
        $this->display();
    }

    public function cat_order()
    {
        if(empty(I('id')))
            $this->error('参数缺失');

        $id=I('id');
        $product_id_list=M('product')->field('id')->where('shop_id='.$id)->select();
        $arr_temp=array();
        foreach ($product_id_list as $k1=>$v1){
            $arr_temp[]=$v1['id'];
        }
        $product_id_str=arr2str($arr_temp);
        $map['product_id']=array('in',$product_id_str);
        $order_id_list=M('order_product')->field('order_id')->where($map)->select();

        $arr_temp2=array();
        foreach ($order_id_list as $k2=>$v2){
            $arr_temp2[]=$v2['order_id'];
        }
        $order_id_str=arr2str($arr_temp2);

        $map2['id']=array('in',$order_id_str);
//        $list=M('order')->where($map2)->select();

        $Order=D('order');
        $list   = $this->lists('Order', $map2);
        foreach ($list as &$item) {
            $item['status_text'] = $Order->getOrderStatusText($item);
            $item['money'] = $item['price']*0.01;
        }

        $this->meta_title='商家订单列表';
        $this->assign('_list',$list);
        $this->display();

    }

    /**
     *
     */
    public function edit(){
        $id = I('id');
        $shop = D('shop')->where('id='.$id)->find();
        if($shop){
            if(IS_POST) {
                D('member')->where('uid='.$shop['uid'])->setField('score',I('post.score'));
                D('shop')->where('id='.$shop['id'])->Save(array(
                    'give_score' => I('post.give_score'),
                    'recover_score' => I('post.recover_score'),
                ));
                $this->success('店铺信息保存成功！',U('manage'));
            }ELSE {
                $shop[ 'score' ] = D ( 'member' )->where ( 'uid=' . $shop[ 'uid' ] )->getField ('score');
                $this->assign ( 'info', $shop );
                $this->meta_title = '店铺信息查看修改';
                $this->display ();
            }
        }else{
            redirect(U('manage'));
        }
    }

    public function remove()
    {
        $shop_id=I('get.id');
        //刪除商家,并且把商家的回收积分转到商户内
        $uid=M('shop')->field('uid')->where('id='.$shop_id)->find();
        $score=M('shop')->field('recover_score')->where('id='.$shop_id)->find();
        $u_score=M('member')->field('score')->where('uid='.$uid['uid'])->find();
        M('member')->where('uid='.$uid['uid'])->setField('score',intval($u_score['score'])+intval($score['recover_score']));
        M('shop')->where('id='.$shop_id)->delete();
        //刪除产品
        M('product')->where('shop_id='.$shop_id)->delete();
        $this->success('商家删除成功！');
    }

    /**
     * 待審覈商家列表
     */
    public function review(){
        $nickname       =   I('get.shopname');
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }
        $map['review']  =   array('eq',0);
        $list=M('shop')->where($map)->order('create_time desc')->select();
        int_to_string($list);
        foreach ($list as &$item){
            $item['score'] = D('member')->where('uid='.$item['uid'] )->getField('score');
        }
        $this->assign('_list', $list);
        $this->assign('_total', count($list));
        $this->meta_title = '待审核商家列表';
        $this->display();
    }

    public function pass_review(){
        $uid=M('Shop')->where(array('id'=>I('get.id')))->getField('uid');
        $this->addShopMessage($uid,20);
        $this->editRow('Shop',array('review'=>1), array('id'=>I('get.id')),array( 'success'=>'通过审核成功！', 'error'=>'状态恢复失败！') );
    }

    public function reject_review(){
        $uid=M('Shop')->where(array('id'=>I('get.id')))->getField('uid');
        $bond = M('bond')->find();
        M('member')->where('uid='.$uid)->setInc('amount', $bond['money']);
        $this->addShopMessage($uid,30);
        $this->editRow('Shop',array('review'=>2), array('id'=>I('get.id')),array( 'success'=>'审核不通过！', 'error'=>'状态恢复失败！') );
    }

    /**
     * 批量处理审核
     *
     * @param null $method
     *
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'reject':
                foreach (explode(',',$id) as $key=>$val){
                    $uid=M('Shop')->where(array('id'=>$val))->getField('uid');
                    $this->addShopMessage($uid,30);
                }
                $this->editRow('Shop',array('review'=>2), $map,array( 'success'=>'审核不通过成功！', 'error'=>'状态恢复失败！') );
                break;
            case 'resume':
                foreach (explode(',',$id) as $key=>$val){
                    $uid=M('Shop')->where(array('id'=>$val))->getField('uid');
                    $this->addShopMessage($uid,20);
                }
                $this->editRow('Shop',array('review'=>1), $map,array( 'success'=>'通过审核成功！', 'error'=>'状态恢复失败！') );
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     * 添加消息通知
     *
     * @param $uid
     * @param $messgae_type
     */
    function addShopMessage($uid,$messgae_type){
        $data=array();
        $data['uid']=$uid;
        $data['type']=3;
        $data['title']='商店消息';
        $data['cover_id']='0';
        $data['read_time']='0';
        $data['create_time']=NOW_TIME;
        switch ($messgae_type){
            case 10:
                $data['message']='尊敬的用户您好，您的店铺正在审核中，请您耐心等待！';
                break;
            case 20:
                $data['message']='尊敬的用户您好，您的店铺已经通过审核，快去发布你的商品吧！';
                break;
            case 30:
                $data['message']='尊敬的用户您好，您的店铺没有通过审核，请联系客服人员！';
                break;
            default:
                break;
        }
        M('message')->add($data);
    }
    /***
     * 保证金列表
     */
    public function bond()
    {
        $result = M('bond')->find();
        $this->assign('result', $result);
        $this->display();
    }
    /**
     * 修改开店保证金
     */
    public function bondedit() {
        if (IS_POST) {
            $money = I('money');
            $result = M('bond')->where('id=1')->setField('money', $money);
            $result?$this->success('操作成功'):$this->error('操作失败');
        } else {
            $result = M('bond')->find();
            $this->assign('result', $result);
            $this->display();
        }
    }
}